package www.ym.com.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.ym.com.entity.Note;
import www.ym.com.mapper.NoteMapper;
import www.ym.com.service.NoteService;


@Service
@Slf4j
public class  NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService{

    @Autowired
    private  NoteMapper noteMapper;

    @Override
    public Page<Note> pageNote(JSONObject jsonObject) {
        //当前页数
        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));
        //每页显示数
        Long pageSize = Long.valueOf(String.valueOf(jsonObject.get("pageSize")));
        //笔记类型
        String type = String.valueOf(jsonObject.get("type"));
        String keyword = String.valueOf(jsonObject.get("keyword"));
        log.info("当前页为"+pageNum);
        log.info("每页显示数"+pageSize);
        log.info("笔记类型"+type);
        log.info("关键字"+keyword);
        LambdaQueryWrapper<Note> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(w->w.like(keyword!=null,Note::getKeyword,keyword))
                .eq(type != "",Note::getType, type);
        queryWrapper.orderByDesc(Note::getCreateTime);

        //进行分页查询-可跟条件
        Page<Note> urf = noteMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        System.out.println("数据为：" + urf.getRecords());
        System.out.println("总数为：" + urf.getTotal() + ",总页数为：" + urf.getPages());
        System.out.println("当前页为：" + urf.getCurrent() + ",每页限制：" + urf.getSize());
        return urf;
    }
}
